﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace KDSugar.Galaxy.API.MetaData
{
    /// <summary>
    /// 元数据查询指令
    /// </summary>
    public class MetaDataCommand : Base.BaseCommand
    {
        /// <summary>
        /// 元数据查询指令URL
        /// </summary>
        public const string METADATA_COMMAND_URL = "/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.QueryBusinessInfo.common.kdsvc";

        /// <summary>
        /// 元数据查询指令初始化
        /// </summary>
        /// <param name="_formId"></param>
        /// <param name="_client">API客户端</param>
        public MetaDataCommand(string _formId,GalaxyCloudAPIClient _client)
            : base(METADATA_COMMAND_URL, _formId, _client)
        {

        }

        /// <summary>
        /// 验证元数据查询返回是否符合规则
        /// </summary>
        /// <param name="_responseData"></param>
        /// <returns></returns>
        public override bool ValidateResponseDataRule(string _responseData)
        {
            bool result = false;
            try
            {
                if (!string.IsNullOrWhiteSpace(_responseData))
                {
                    Result.MDReturnInfo returnInfo = JsonConvert.DeserializeObject<Result.MDReturnInfo>(_responseData);
                    if (returnInfo != null)
                    {
                        var r1 = returnInfo.Result;
                        if (r1 != null)
                        {
                            var r2 = r1.ResponseStatus;
                            if (r2 != null)
                            {
                                result = r2.IsSuccess;
                            }
                        }
                    }
                }
            }
            catch
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 业务对象表单Id
        /// </summary>
        public string FormId
        {
            get
            {
                return GetFormId();
            }
        }

        //private class MateExecuteExcept : Exception
        //{
        //    public string AppendMessage { get; set; }
        //}

        /// <summary>
        /// 执行元数据查询指令
        /// </summary>
        /// <returns></returns>
        public Result.MDInfo ExecuteMetaDataQuery()
        {
            Result.MDInfo result = null;
            string errorMsg = "执行发生未知错误";
            var resultPack = APIClient.ExecuteCommand(this);
            bool isSuccess = false;
            if (resultPack != null)
            {
                if (resultPack.IsResponseSuccess)
                {
                    string mexMsg = "未知错误";
                    try
                    {
                        mexMsg = "返回数据反序列化对象出错|";
                        mexMsg += !string.IsNullOrWhiteSpace(resultPack.ResponseData) ? resultPack.ResponseData : "返回数据为空";
                        Result.MDReturnInfo returnInfo = JsonConvert.DeserializeObject<Result.MDReturnInfo>(resultPack.ResponseData);
                        if (returnInfo != null)
                        {
                            var r = returnInfo.Result;
                            if (r != null)
                            {
                                if (r.NeedReturnData != null)
                                {
                                    isSuccess = true;
                                    result = r.NeedReturnData;
                                }
                                else
                                {
                                    errorMsg = "元数据信息为NULL";
                                    throw new Exception(errorMsg);
                                }
                            }
                            else
                            {
                                mexMsg = "元数据返回信息（Result）为NULL";
                                throw new Exception(mexMsg);
                            }
                        }
                        else
                        {
                            mexMsg = "返回数据反序列化后对象为NULL";
                            throw new Exception(mexMsg);
                        }
                    }
                    catch(Except.ExecuteException ex)
                    {
                        ex.OperateCommandName = GetOperateCommandName();
                        ex.AppendMessage = mexMsg;
                        ex.ExecuteData = resultPack;
                        throw ex;
                    }
                }
                else
                {
                    errorMsg = "执行调用元数据返回数据检查为false";
                }
            }
            else
            {
                errorMsg = "执行调用元数据API返回数据为空";
            }
            if (!isSuccess)
                throw new Exception(errorMsg);
            return result;
        }
    }
}
